import 'package:dk_flutter_base/dk_core.dart';
import 'package:dk_flutter_base/dk_ui.dart';

typedef ConfigControllerListener(TabController controller);

class BaseHomePage extends StatefulWidget {
  ///首页构建参数
  final BaseHomePageParam param;

  @override
  State<BaseHomePage> createState() {
    return getState();
  }

  ///通过content的View列表以及底部的tab列表
  BaseHomePage.createByParam(this.param, {Key key}) : super(key: key);

  State<BaseHomePage> getState() {
    switch (param?.bottomBarParam?.bottomBarType) {
      case BottomBarType.tab:
        return _BaseTabHomePageState();
      case BottomBarType.navigation:
      default:
        return _BaseNavigationHomePageState();
    }
  }
}

class _BaseTabHomePageState extends State<BaseHomePage>
    with SingleTickerProviderStateMixin {
  TabController controller;
  Widget contentView;

  @override
  void initState() {
    super.initState();
    //通过外部widget创建
    controller = TabController(
        length: widget?.param?.contentWidgets?.length,
        vsync: this,
        initialIndex: widget?.param?.initialIndex);

    var configControllerListener = widget?.param?.configControllerListener;
    if (null != configControllerListener) {
      configControllerListener(controller);
    }
    widget?.param?.bottomBarParam?.tabBarOption?.controller = controller;
    contentView = TabBarView(
      children: widget?.param?.contentWidgets,
      controller: controller,
      physics: widget?.param?.contentPhysics,
    );
  }

  @override
  void dispose() {
    controller?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: widget?.param?.appBar,
      body: contentView,
      bottomNavigationBar:
          DkBottomAppBar.createByParam(widget?.param?.bottomBarParam),
    );
  }
}

class _BaseNavigationHomePageState extends State<BaseHomePage> {
  int _currentIndex = 0;

  @override
  void initState() {
    super.initState();
    _currentIndex = widget?.param?.initialIndex ?? 0;
    widget?.param?.bottomBarParam?.bottomNavigationBarOption?.onTap =
        (int index) {
      setState(() {
        _currentIndex = index;
        widget?.param?.bottomBarParam?.bottomNavigationBarOption?.currentIndex =
            _currentIndex;
      });
    };
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: widget?.param?.appBar,
      body: IndexedStack(
        index: _currentIndex,
        children: widget?.param?.contentWidgets,
      ),
      floatingActionButton: widget?.param?.bottomBarParam
          ?.bottomNavigationBarOption?.floatingActionButton,
      floatingActionButtonLocation: widget?.param?.bottomBarParam
          ?.bottomNavigationBarOption?.floatingActionButtonLocation,
      bottomNavigationBar:
          DkBottomAppBar.createByParam(widget?.param?.bottomBarParam),
    );
  }
}

class BaseHomePageParam {
  ///标题栏控件
  PreferredSizeWidget appBar;

  ///tabContent 的所有widget 与下面的tabViewRoutes互斥
  List<Widget> contentWidgets;

  ScrollPhysics contentPhysics;

  ///初始显示tabContent的下标
  int initialIndex;

  ///底部导航栏构建参数
  BottomAppBarParam bottomBarParam;

  ConfigControllerListener configControllerListener;

  ///通过tab的路由集合及底部导航栏创建page
  BaseHomePageParam.withTabRoute(
    List<MapEntry<String, Map<String, dynamic>>> tabRoutes,
    this.bottomBarParam, {
    this.initialIndex = 0,
    this.configControllerListener,
    this.appBar,
    this.contentPhysics,
  }) {
    getWidgetsByRoute(tabRoutes);
    contentPhysics ??= NeverScrollableScrollPhysics();
  }

  ///通过tab的Widget集合及底部导航栏创建page
  BaseHomePageParam.withTabWidgets(
    this.contentWidgets,
    this.bottomBarParam, {
    this.initialIndex = 0,
    this.configControllerListener,
    this.appBar,
    this.contentPhysics,
  }) {
    contentPhysics ??= NeverScrollableScrollPhysics();
  }

  ///通过Float的路由集合及底部导航栏创建page
  BaseHomePageParam.withNavigationRoute(
    List<MapEntry<String, Map<String, dynamic>>> tabRoutes,
    this.bottomBarParam, {
    this.initialIndex = 0,
    this.appBar,
  }) {
    getWidgetsByRoute(tabRoutes);
  }

  ///通过Float的Widget集合及底部导航栏创建page
  BaseHomePageParam.withNormalWidgets(
    this.contentWidgets,
    this.bottomBarParam, {
    this.initialIndex = 0,
    this.appBar,
  });

  void getWidgetsByRoute(
      List<MapEntry<String, Map<String, dynamic>>> tabRoutes) {
    contentWidgets = List<Widget>();
    if (null == tabRoutes || tabRoutes.length <= 0) {
      return;
    }
    tabRoutes.forEach((MapEntry<String, Map<String, dynamic>> mapEntry) {
      if (null != mapEntry) {
        contentWidgets.add(
            RouteUtil.getWidget(mapEntry.key, widgetParams: mapEntry.value));
      }
    });
  }
}
